"""
Test minigame.

Player is given a choice between winning or losing.

"""

#  Copyright 2010 Nick Ryder.

#  This file is part of Minigame Mayhem.

#  Minigame Mayhem is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  Minigame Mayhem is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

#  You should have received a copy of the GNU General Public License
#  along with Minigame Mayhem.  If not, see <http://www.gnu.org/licenses/>.

import minigame
import pygame
from pygame.locals import *
import time
import sys

class TestMinigame(minigame.Minigame):

   def __init__(self, screen, level=0):
      self.gametime = 5       # 10 seconds to play
      self.developers = ["Nick Ryder"]
      self.testers = ["Nick Ryder", "Amy Kolpin"]
      self.artists = ["Ha."]
      minigame.Minigame.__init__(self, screen, level)

   def start(self):
      "Welcome message."
      self.background = pygame.Surface(self.screen.get_size())
      self.background.convert()
      self.background.fill((50, 50, 50))
      text = "Test Minigame"
      titleFont = pygame.font.Font(None, 40)
      text = titleFont.render(text, 1, (0, 0, 0))
      textpos = text.get_rect(centerx=self.background.get_width()/2,
         centery=self.background.get_height()/2)
      self.background.blit(text, textpos)
      instructions = "Press the button to win."
      instrFont = pygame.font.Font(None, 30)
      instructions = instrFont.render(instructions, 1, (0, 0, 0))
      instrpos = instructions.get_rect(centerx = self.background.get_width()/2,
         midtop=textpos.midbottom)
      self.background.blit(instructions, instrpos)
      self.screen.blit(self.background, (0,0))
      pygame.display.flip()
      time.sleep(1)

   def main(self):
      "Main game. Player chooses to win or lose."
      font = pygame.font.Font(None, 25)
      wintext = font.render("Win", 1, (0, 0, 0))
      winrect = wintext.get_rect()
      winsurface = pygame.Surface(winrect.size)
      winsurface.fill((200, 0, 0))
      winsurface.blit(wintext, (0, 0))
      winbutton = winsurface.get_rect(center=self.background.get_rect().center)
      self.background.fill((50, 50, 50))
      self.background.blit(winsurface, winbutton)
      starttime = time.time()
      currenttime = time.time()
      timeremaining = self.gametime - (currenttime - starttime) 
      timetext = font.render(str(int(timeremaining + 1)), 1, (0, 0, 0))
      timerect = timetext.get_rect()
      timesurface = pygame.Surface(timerect.size)
      timesurface.fill((50, 50, 50))
      timesurface.blit(timetext, timerect)
      timerect = timesurface.get_rect(bottom=self.background.get_height(),
         right = self.background.get_width())
      self.background.blit(timesurface, timerect)
      self.screen.blit(self.background, (0, 0))
      pygame.display.flip() 
      while timeremaining > 0:
         currenttime = time.time()
         timeremaining = self.gametime - (currenttime - starttime) 
         timetext = font.render(str(int(timeremaining + 1)), 1, (0, 0, 0))
         timesurface.fill((50, 50, 50))
         timesurface.blit(timetext, (0, 0))
         self.background.blit(timesurface, timerect)
         self.screen.blit(self.background, (0, 0))
         pygame.display.flip()
         for event in pygame.event.get():
            if event.type == QUIT:
               sys.exit()  # Not sure this is good
            elif event.type == KEYDOWN:
               if event.key == K_ESCAPE:
                  sys.exit() 
            elif event.type == MOUSEBUTTONDOWN:
               x, y = pygame.mouse.get_pos()
               if winbutton.collidepoint(x, y):
                  self.win = True 
                  timeremaining = -1

   def finish(self):
      "Congratulate or commiserate player."
      self.background = pygame.Surface(self.screen.get_size())
      self.background.convert()
      self.background.fill((50, 50, 50))
      text = "Test Minigame"
      titleFont = pygame.font.Font(None, 40)
      text = titleFont.render(text, 1, (0, 0, 0))
      textpos = text.get_rect(centerx=self.background.get_width()/2,
         centery=self.background.get_height()/2)
      self.background.blit(text, textpos)
      if self.win:
         result = "You won."
      else:
         result = "You lost."
      resultFont = pygame.font.Font(None, 30)
      result = resultFont.render(result, 1, (0, 0, 0))
      resultpos = result.get_rect(centerx = self.background.get_width()/2,
         midtop=textpos.midbottom)
      self.background.blit(result, resultpos)
      self.screen.blit(self.background, (0,0))
      pygame.display.flip()
      time.sleep(1)
